package com.example.first.thread;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @Author LSQ
 * @Description ExecutorService extends Executor, ThreadPoolExecutor是具体实现类
 * @date 2021/8/5 9:13
 * @Version 1.0
 */

/**
 * ExecutorService 是线程池管理工具类，一般就负责提交线程，判断线程池的状态，没有其它功能
 */

public class LearnExecutor0 {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        Runnable runnable = () -> {
            System.out.println("running...");
            try {
                TimeUnit.SECONDS.sleep(6);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        executorService.submit(runnable);
        executorService.shutdown();
        // 不会立即关闭，要等所有线程都执行完成
        System.out.println("print thread pool is shutdown: " + executorService.isShutdown());
        System.out.println("print thread pool is Terminated: " + executorService.isTerminated());

        while (true) {
            System.out.println("executorService is running...");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (executorService.isTerminated()) {
                System.out.println("executorService is terminated...");
                break;
            }

        }


    }
}
